<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;

class Common extends Model {

    /**
     * 指示是否自动维护时间戳
     *
     * @var bool
     */
    public $timestamps = false;

    //根据model class获取表名 表名中包含_，用驼峰自动转换
    public function getTable() {
        return $this->table ? $this->table : strtolower(Str::snake(class_basename($this)));
    }

    protected $casts = [
        'ctime' => 'datetime:Y-m-d',
    ];

    public function getCommentList($field, $where = [], $pageSize = 15) {
        $list = self::select($field)->where($where)->orderBy('id', 'desc')->paginate($pageSize);

        foreach ($list as $k => $v) {
            $res = self::where(['pid' => $v['id']])->get();
            if (!empty($res)) {
                $list[$k]['toUser'] = $res;
            } else {
                $list[$k]['toUser'] = null;
            }
        }

        return $list;
    }

}
